﻿

$(function () {
    _vmBase = new viewModel();

    _vmBase.init();


})

function viewModel() {
    var me = this;
    VueViewModelBase.call(me);
    viewModelExtend.call(me);


    me.tableListUrl = _getTableListUrl;
    me.addUrl = _updateModelUrl;
    me.delUrl = _deleteModelUrl;
    me.tabObj = $('#tb_categories');
    

    //重写方法
    me.init = function () {


        me.initTableList();

        //添加监听对象
        me.addMonitor();

        //me.bindCategoryCode();

        me.InitControl();

    }

    me.initTableList = function () {
        var options = {
            search: false,
            //showFilter: true,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'name',
                    title: '名称',
                    filterControl:"input"
                },
                {
                    field: 'categoryCode',
                    title: '类型',
                    filterControl: "select",
                    formatter: function (value, row, index) {
                        if (row.categoryCode == categoryResourceType) {
                            return "资源";
                        } else if (row.categoryCode == categoryDicType) {
                            return "字典";
                        }
                    }
                },
                {
                    field: 'code',
                    title: '编码',
                    filterControl: "input"
                },
                {
                    field: 'parentCode',
                    title: '父编码',
                    filterControl: "input"
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        var enabledLabel = '<span class="label label-success" id="' + row.id + '"><i class="fa fa-unlock"></i>' + _enabledStr + '</span>';
                        var disabledLabel = '<span class="label label-default" id="' + row.id + '"><i class="fa fa-lock"></i>' + _disabledStr + '</span>';
                        if (row.status) {
                            return enabledLabel;
                        } else {
                            return disabledLabel;
                        }
                    }
                },
                {
                    field: 'id',
                    title: '操作',
                    formatter: function (value, row, index) {

                        var enabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onEnabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledResourceUrl + '\',\'' + _disabledResourceUrl + '\',event)">' + _enabledStr + '</a>';
                        var disabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onDisabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledResourceUrl + '\',\'' + _disabledResourceUrl + '\',event)">' + _disabledStr + '</a>';

                        if (row.status) {
                            return disabledEle;
                        } else if (!row.status) {
                            return enabledEle;
                        }

                    }
                }
            ],
            filterControl: true,
            //filterTemplate: {}, //修改了bootstrap-table-filter-control.js源码,placeholder默认为undefined,不会渲染input
            queryParams: function (params) {
                var filter = "";
                if (params.filter != undefined && params.filter != null) {
                    filter = params.filter;
                }
                //console.log(params);
                var temp = {
                    rows: params.limit,                         //页面大小
                    page: (params.offset / params.limit) + 1,   //页码
                    queryKey: vueModel.queryKey,
                    treeId: vueModel.treeId,
                    filter: filter
                };
                return temp;
            }
        }
        me.initList(options);

    }


    me.add = function () {
        vueModel.$set(vueModel.entityObj, "ParentName", "根编码");
    }

    //重写了父方法
    //用于普通 下拉框
    //me.edit = function () {
                
    //    if (vueModel.selectedData) {
    //        var selectRows = me.selections;

    //        var lastSelectObj = selectRows[selectRows.length - 1];

    //        me.bindParentCode(lastSelectObj.categoryCode, function (data) {

    //            //根据资源类型，加载父编码，加载成功后，绑定值到文本框
    //            me.addMonitor(lastSelectObj);
    //        });

    //    } else {
    //        $dh.msg(_SelectADataMsg);//请选择一条数据
    //    }

    //}

}
function viewModelExtend() {
    var me = this;

    me.InitControl = function () {
        me.bindDicTree();

        me.bindEventToBtn();
    }

    me.bindDicTree = function () {
        $('#categoryCode').on('change', function () {
            var selectValue = $('#categoryCode option:selected').val();

            me.bindParentTreeCode(selectValue);
        });

        $("#ParentName").on("click", function () {
            _parentDicTree.reload();
        });
        vueModel.$set(vueModel.entityObj, "ParentName", "根编码");
        _parentDicTree = new ParentTree(_getDicDefaultTreeUrl, "ParentName", "ParentCode", "parentDicTree", vueModel.entityObj);

    }
    me.bindParentTreeCode = function (categoryType) {
        if (categoryType == categoryDicType) {

            if (me.addOp) {
                vueModel.$set(vueModel.entityObj, "ParentName", "");
            }

            _parentDicTree = new ParentTree(_getDicTreeUrl, "ParentName", "ParentCode", "parentDicTree", vueModel.entityObj);

        } else if (categoryType == categoryResourceType) {

            vueModel.$set(vueModel.entityObj, "ParentName", "根编码");
            _parentDicTree = new ParentTree(_getDicDefaultTreeUrl, "ParentName", "ParentCode", "parentDicTree", vueModel.entityObj);
        }


    }


    me.addMonitor = function (monitorObj) {
        var targetObj = vueModel.entityObj;

        if (monitorObj != null && monitorObj != undefined) {
            me.addMonitorObj(targetObj, monitorObj);
        } else {
            var addObj = {
                Id: '',
                Name: '',
                Status: true,
                ParentCode: 'DH',
                ParentName:'',
                CategoryCode: defaultCategoryCode,
                Code: '',
                Description: '',
                CreatedBy: '',
                UpdatedBy: ''
            };
            me.addMonitorObj(targetObj, addObj);
        }
    }

    //普通select 
    me.bindCategoryCode = function () {
        $('#parentCode').append('<option value="DH">==请选择==</option>');

        $('#categoryCode').on('change', function () {
            var selectValue = $('#categoryCode option:selected').val();

            me.bindParentCode(selectValue, function (data) { });
        });
    }

    me.bindParentCode = function (categoryType,callback) {
        if (categoryType == categoryDicType) {
            $('#parentCode').html("");
            //获取字典
            $dh.get(_getDicsUrl, null, function (result) {

                var options = "";
                $('#parentCode').append('<option value="DH">==请选择==</option>');
                for (dic in result) {
                    options += '<option value="' + result[dic].code + '">' + result[dic].name + '</option>';
                }
                $('#parentCode').append(options);

                callback(true);
            });


        } else if (categoryType == categoryResourceType) {

            $('#parentCode').html("");
            $('#parentCode').append('<option value="DH">==请选择==</option>');

            callback(true);
        }


    }

    //初始化系统资源
    me.InitResource = function () {
        $dh.get(_initResource, null, function (result) {
            if (result.Status == ResultStatus.OK) {
                $dh.msg(result.Msg);
                _vmBase.search();
            } else {
                $dh.alert(result.Msg);
            }

        });
    }

    //给功能按钮绑定事件
    me.bindEventToBtn = function () {
        var initCategoryId = "Category_Index_btnInit";
        var $obj = $('#' + initCategoryId);
        if ($obj){
            var innerHtml = $obj.html();
            var sliceIndex = innerHtml.indexOf('</i>');
            if (sliceIndex>0){
                innerHtml = innerHtml.substr(0, sliceIndex + 4);
                innerHtml += _InitResourceStr;
                $obj.html(innerHtml);
                $obj.on("click", me.InitResource);
            }
            
        }
    }
    
}
